import {reactive} from "vue";


class Store {

    constructor(options) {
        this.$options = options
        this.state = reactive(this.$options.state)

        this.commit = this.commit.bind(this)
        this.dispatch = this.dispatch.bind(this)
    }


    install(app) {
        app.config.globalProperties.$store = this
    }

    commit(type, payload) {
        const entry = this.$options.mutations[type]
        if(entry)
            entry(this.state, payload)
    }

    dispatch(type, payload) {
        const entry = this.$options.actions[type]
        if(entry)
            entry(this, payload)
    }
}

export function createStore(options) {
    return new Store(options)
}
